package com.xiaodao.springboot.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaodao.springboot.config.ApiResponse;
import com.xiaodao.springboot.entity.User;
import com.xiaodao.springboot.services.TechnicianService;
import com.xiaodao.springboot.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;


    @Autowired
    private TechnicianService technicianService;

    @GetMapping("/testjoin")
    public ApiResponse testjoin() {

        return ApiResponse.ok();
    }


    // 查询全部用户（分页）
    @GetMapping
    public Page<User> getAllUsers(@RequestParam(defaultValue = "1") int pageNum,
                                  @RequestParam(defaultValue = "10") int pageSize) {
        return userService.page(new Page<>(pageNum, pageSize));
    }

    // 新增用户
    @PostMapping
    public ApiResponse addUser(@RequestBody User user) {
        user.setCreatedTime(LocalDateTime.now());
        boolean userid=userService.save(user);
        return ApiResponse.ok(userid);
    }

    // 更新用户
    @PutMapping("/{id}")
    public ApiResponse updateUser(@PathVariable Long id, @RequestBody User user) {
        user.setId(id+"");
        user.setUpdateTime(LocalDateTime.now());
        boolean userid=userService.updateById(user);
        return ApiResponse.ok(userid);
    }

    // 删除用户
    @DeleteMapping("/{id}")
    public ApiResponse deleteUser(@PathVariable Long id) {
        //return ApiResponse.ok(userService.removeById(id));
        return ApiResponse.ok();
    }
}